package cc.rengu.redp.bizimpl.igas.controller;


import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.igas.domain.SEventInfo;
import cc.rengu.redp.bizimpl.igas.domain.SEventInfo;
import cc.rengu.redp.bizimpl.igas.service.PDstChannelCfgService;
import cc.rengu.redp.bizimpl.igas.service.SEventInfoService;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 刘玉亮.LL
 */
@Slf4j
@Api(tags = "商户事件指派")
@RestController
@RequestMapping("/admin/igas/eventInfo")
public class SEventInfoController extends BaseController {
    @Autowired
    private SEventInfoService sEventInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "declareDateStart", value = "开始日期", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "declareDateEnd", value = "结束日期", dataType = "String")
    })
    public Map<String, Object> list(QueryRequest request, SEventInfo entity, String declareDateEnd, String declareDateStart) {
        return getDataTable(this.sEventInfoService.findByPage(request, entity, declareDateEnd, declareDateStart));
    }

    @GetMapping(value = "/edit")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "事件编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public SEventInfo edit(String id, String instId){
        LambdaQueryWrapper<SEventInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(SEventInfo::getInstId, instId);
        wrapper.ge(SEventInfo::getEventId, id);
        SEventInfo entity = sEventInfoService.getOne(wrapper);
        entity.setMchntName(sEventInfoService.getMchntName(entity.getMchntNo()));
        return entity;
    }

    @GetMapping(value = "/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "事件编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String")
    })
    public Map<String,Object> detail(String id, String instId){
        return sEventInfoService.detail(id,instId);
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "instId", value = "机构标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dstChannelId", value = "通道标识", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dstChannelName", value = "通道名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "optFlag", value = "操作类型：新增、修改、删除、审核等", required = true, dataType = "String")
    })
    public void update(@Valid SEventInfo entity) throws RedpException {
        try {
            this.sEventInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }
}
